import { getLimitArea, getLayerConfigs } from '@/api/essential/map'

const state = () => ({
  mapModeList: [
    { label: '全图模式', value: '1', icon: 'quantu' },
    { label: '卷帘模式', value: '-1', icon: 'juanlian' },
    { label: '双图模式', value: '2', icon: 'shuangtu' },
    { label: '切换模式', value: '3', icon: 'qiehuan' }
  ],
  // 期别
  stageList: [
    // { label: '期别1', value: '1' },
    // { label: '期别2', value: '2' },
    // { label: '期别3', value: '3' }
  ],
  // 默认期别
  defaultStage: '3',
  // 地图浮层禁用状态
  popupDisabled: false,
  // 地图浮层图片
  popupImgList: [],
  // 无人机禁飞区
  forbidFlyArea: [],
  // 无人机限高区
  limitHeightArea: [],
  // 鼠标坐标
  pointer: [0, 0],
  // 地图层级
  zoom: 0,
  // 后端返回的地图图层配置
  layerConfigs: [],
  // 全局地图所有图层的层级
  layerZIndexMap: {},
  // 初始化动态图层的层级
  layerTypeLastZIndexMap: {
  },
  // 初始化地图图层的层级
  initLayerZIndexMap: window.serverConfigs?.initLayerZIndexMap
})

const mutations = {
  setStageList (state, list) {
    state.stageList = list
  },
  setDefaultStage (state, stage) {
    state.defaultStage = stage
  },
  togglePopupDisabled (state) {
    state.popupDisabled = !state.popupDisabled
  },
  setPopupImgList (state, payload) {
    state.popupImgList = payload
  },
  setForbitFlyArea (state, payload) {
    state.forbidFlyArea = payload
  },
  setLimitHeightArea (state, payload) {
    state.limitHeightArea = payload
  },
  setPointer (state, payload) {
    state.pointer = payload
  },
  setZoom (state, payload) {
    state.zoom = payload
  },
  setlayerConfigs (state, payload) {
    state.layerConfigs = payload
  },
  updateLayerZIndex (state, { layerId, zIndex }) {
    state.layerZIndexMap[zIndex] = layerId
  }
}

const actions = {
  async getArea ({ commit }, extent) {
    const { code, data } = await getLimitArea(extent)
    if (code === 0) {
      const forbidArea = []
      const limitHeightArea = []
      for (const item of data.areas) {
        if (item.sub_areas && item.sub_areas.length) {
          for (const a of item.sub_areas) {
            if (!a.polygon_points) continue
            if (a.color === '#DE4329') {
              forbidArea.push(a.polygon_points)
            } else if (a.color === '#979797') {
              limitHeightArea.push(a.polygon_points)
            }
          }
        }
      }
      commit('setForbitFlyArea', forbidArea)
      commit('setLimitHeightArea', limitHeightArea)
    }
  },
  async getLayers ({ commit }, params) {
    const data = await getLayerConfigs(params)
    const { layers } = layersProcessor(data)
    commit('setlayerConfigs', layers)
  }
}

export function flattenList (source, target) {
  for (const r of source) {
    const { children, ...others } = r
    target.push({
      isLeaf: !children,
      children,
      ...others
    })
    if (children && children.length) {
      flattenList(children, target)
    }
  }
}

export function layersProcessor (treeData) {
  const result = []
  flattenList(treeData, result)
  const layers = result.filter(item => item.att1 === '1').map((item, index) => {
    const params = item.att7 ? JSON.parse(item.att7) : {}
    const parent = result.find(r => r.id === item.fid)
    return {
      bklayerId: item.id,
      bklayerType: item.att2,
      bklayerName: item.label,
      bklayerUrl: item.att4,
      bklayerSn: parent.num1 + item.order,
      bklayerGroupId: item.fid,
      visible: item.att3 === '1',
      isHidden: item.att6 === '0',
      icon: item.iconCls,
      center: item.num5 && item.num6 ? [item.num5, item.num6] : null,
      extent: item.num1 && item.num2 && item.num3 && item.num4 ? [item.num1, item.num2, item.num3, item.num4] : null,
      flyTo: item.att5 === '1',
      ...params
    }
  })
  return { layers, list: result }
}

export function restoreRawLayerData (data) {
  const { bklayerId, bklayerName, bklayerGroupId } = data
  // 还原树所需要的最简单程度的结构
  return {
    fid: bklayerGroupId,
    id: bklayerId,
    att1: '1',
    label: bklayerName,
    layer: data
  }
}

export default { state, mutations, actions }
